package com.mumu.jetpackdemo.page;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModel;
import android.arch.paging.DataSource;
import android.arch.paging.LivePagedListBuilder;
import android.arch.paging.PageKeyedDataSource;
import android.arch.paging.PagedList;
import android.arch.paging.PositionalDataSource;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

/**
 * edie create on 2019/3/29
 */
public class My2ViewModel extends ViewModel {
    private LiveData<PagedList<String>> mLiveData;


    public LiveData<PagedList<String>> getLive() {
        if (mLiveData == null) {

//            pageSize：设置每页加载的数量
//            prefetchDistance：预加载的数量，默认为pagesize
//            initialLoadSizeHint：初始化数据时加载的数量，默认为pageSize*3
//            enablePlaceholders：当item为null是否使用PlaceHolder展示

            PagedList.Config config = new PagedList.Config.Builder()
                    //设置每页加载的数量
                    .setPageSize(20)
                    //预加载的数量，默认为pagesize
                    .setPrefetchDistance(3)
                    //初始化数据时加载的数量，默认为pageSize*3
                    .setInitialLoadSizeHint(20)
                    //当item为null是否使用PlaceHolder展示
                    .setEnablePlaceholders(true)
                    .build();

            DataSource.Factory<Integer, String> factory = new DataSource.Factory<Integer, String>() {

                @Override
                public DataSource<Integer, String> create() {
                    return new PositionalDataSource<String>() {
                        @Override
                        public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback<String> callback) {
                            callback.onResult(loadData(0, 20), 0, 200);
                        }

                        @Override
                        public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback<String> callback) {
                            callback.onResult(loadData(params.startPosition, params.loadSize));
                        }
                    };
                }
            };

            mLiveData = new LivePagedListBuilder<>(factory, config).build();
        }
        return mLiveData;


    }

    /**
     * 假设这里需要做一些后台线程的数据加载任务。
     *
     * @param startPosition
     * @param count
     * @return
     */
    private List<String> loadData(int startPosition, int count) {
        List<String> list = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            int i1 = startPosition + i + 1;
            list.add("测试的内容=" + i1);
        }

        return list;
    }

    public LiveData<PagedList<String>> getLive1() {
        if (mLiveData == null) {

//            pageSize：设置每页加载的数量
//            prefetchDistance：预加载的数量，默认为pagesize
//            initialLoadSizeHint：初始化数据时加载的数量，默认为pageSize*3
//            enablePlaceholders：当item为null是否使用PlaceHolder展示

            PagedList.Config config = new PagedList.Config.Builder()
                    //设置每页加载的数量
                    .setPageSize(20)
                    //预加载的数量，默认为pagesize
                    .setPrefetchDistance(3)
                    //初始化数据时加载的数量，默认为pageSize*3
                    .setInitialLoadSizeHint(20)
                    //当item为null是否使用PlaceHolder展示
                    .setEnablePlaceholders(true)
                    .build();

            PageKeyedDataSource.Factory<Integer, String> factory = new PageKeyedDataSource.Factory<Integer, String>() {

                @Override
                public DataSource<Integer, String> create() {
                    return new PageKeyedDataSource<Integer, String>() {
                        @Override
                        public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, String> callback) {
                            callback.onResult(loadData(0, 20),0,200,params.requestedLoadSize,params.requestedLoadSize+1);
                        }

                        @Override
                        public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, String> callback) {
                        }

                        @Override
                        public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, String> callback) {

                        }
                    };
                }
            };
        }

        return mLiveData;

    }
}
